<template>
  <div>
    <van-nav-bar
        left-arrow
        @click-left="$router.replace('/hScmlvxSUkDQ/UjRzDSClllpS')"
        :title="$t('确认借款')"
    >
      <template #left>
        <van-icon name="arrow-left" class="nav_left_icon" size="18px"/>
      </template>
    </van-nav-bar>
    <div class="wrapper">
      <div class="product_box">
        <div class="product_header">
          <div>
            <img class="product_Logo" :src="loanInfo.productLogo"> <span style="vertical-align: middle">{{ loanInfo.appName }}</span>
          </div>
          <span class="amount" style="color: ">₱{{ loanInfo.loanAmount }}</span>
        </div>
        <div class="loan_info">
          <div class="loan_info_item">
            <span style="color: #7C7C7C">{{ $t('借款天数') }}</span>
            <span style="float:right;">{{ loanInfo.loanTerm }} day</span>
          </div>
          <div class="loan_info_item">
            <span style="color: #7C7C7C">{{ $t('借款金额') }}</span>
            <span style="float:right;color: #FAC816">₱{{ loanInfo.loanAmount }}</span>
          </div>
          <div v-show="productFeeShow" class="fee_content">
            <div v-show="productInfoShow.serviceFee === 1">
              <span>{{ $t('服务费') }}</span>
              <span style="float:right;">₱{{ loanInfo.serviceFee }}</span>
            </div>
            <div v-show="productInfoShow.interestFee === 1">
              <span>{{ $t('利息') }}</span>
              <span style="float:right;">₱{{ loanInfo.shouldInterest }}</span>
            </div>
            <div v-show="productInfoShow.receivedAmount === 1">
              <span>{{ $t('到手金额') }}</span>
              <span style="float:right;">₱{{ loanInfo.paymentAmount }}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="bank_card_box" @click="selectBankClick">
        <img class="loan_bank_icon" src="../../assets/my/my_icon_card.png">
        <div class="loan_card_num">
          <div>{{ bankInfo.bankcardName }}</div>
          <div>{{ bankInfo.bankcard }}</div>
        </div>
        <div class="change_right_icon">
          <img class="loan_card_right" src="../../assets/my/loan_card_right.png">
        </div>
      </div>
      <div class="product_box" v-if="planList.length !== 0" style="margin-top: 20px">
        <div class="product_header">
          <div>
            <span>Season2</span> <span style="margin-left: 15px">180 days</span>
          </div>
        </div>
        <div class="loan_info">
          <div class="loan_info_item" v-for="item in planList" :key="item.refundAmount">
            <div style="margin-bottom: 10px">
              <span>Issue {{ item.seq }}</span> <span
                style="float: right;color: #fac816">₱{{ item.refundAmount }}</span>
            </div>
            <div style="color: #BCBCBC">
              <span>Repayment date</span> <span style="float: right">{{ item.refundDate }}</span>
            </div>
          </div>
          <div class="tips_text">* If you pay off the first installment on time, the second installment will be waived</div>
          </div>
        </div>
    </div>
    <div class="confirm_btn">
      <van-button style="width: 80%" type="primary" round @click="apply">{{ $t('提交提现') }}</van-button>
    </div>
  </div>
  <!-- 确认收款账户-->
  <van-popup v-model:show="confirmShow">
    <template #default>
      <div class="dialog_body">
        <div class="title">Confirm the receiving bank account</div>
        <div class="cell_content_list" style="padding: 20px;margin-top: 10px">
          <!--        账户类型-->
          <div class="cell_content_item">
            <div class="text" style="text-align: left">
              Account Type
            </div>
            <div style="position: relative">
              <input class="input" v-model="bankInfo.accountTypeName" :readonly="true"/>
            </div>
          </div>
          <!--        银行名称-->
          <div class="cell_content_item">
            <div class="text" style="text-align: left">
              {{ $t('银行名称') }}
            </div>
            <div style="position: relative">
              <input class="input" v-model="bankInfo.bankcardName" :readonly="true"/>
            </div>
          </div>
          <!--        银行账户-->
          <div class="cell_content_item" style="margin-bottom: 0">
            <div class="text" style="text-align: left">
              Accounts receivable
            </div>
            <div style="position: relative">
              <input class="input" v-model="bankInfo.bankcard" :readonly="true"/>
              <img class="input_icon" src="../../assets/my/input_right.png">
            </div>
          </div>

        </div>
        <div class="add_card_text" @click="$router.push('/ZMyMfdSBUti')">
          <span style="vertical-align: middle;">{{ $t('添加收款银行卡') }}</span>
          <van-icon name="arrow" style="vertical-align: middle"/>
        </div>
        <div style="margin-top: 20px">
          <van-button style="margin-right: 10%;width: 35%" @click="confirmShow = false" round>Reselect</van-button>
          <van-button type="primary" @click="apply" style="width: 35%" round>Sure</van-button>
        </div>
      </div>
    </template>
  </van-popup>
  <!-- 好评弹窗-->
  <!--  -->
  <van-action-sheet
      v-model:show="bankCardShow"
      :actions="selectBankList"
      @select="selectBankConfirm"
      :cancel-text="$t('取消')"
      close-on-click-action
  />
</template>

<script>
import {useStore} from 'vuex'
import {onMounted, reactive, ref} from "vue";
import {applyConfirm, fetchLoanProductDetails, fetchMyCard, getHomepageLimit, upDateMyCard} from "@/api/home";
import {Toast} from "vant";
import router from "@/router";
import {useI18n} from "vue-i18n";
import {useRoute} from 'vue-router'
import {setResourcesShow, formatDate_EN, getAndroidUploadResult, upLoadAdjustPoint} from "@/utils/tool";

export default {
  name: "index",
  setup() {
    const store = useStore()
    const productNameMain = localStorage.getItem('productName')
    const expandInfoShow = ref(true)
    const confirmShow = ref(false)
    const downTime = ref(10 * 60 * 1000);
    const {productName} = useRoute().query
    const {t} = useI18n()
    const loanInfo = ref({})
    const productInfoShow = ref({})
    const productFeeShow = ref(true)
    const selectBankList = ref([])
    const planList = ref([])
    const bankInfo = reactive({
      bankcardName: null,
      bankcard: null,
      accountTypeName: null
    })
    const bankCardShow = ref(false)

    // 立即申请
    async function apply() {
      const isResult = await getAndroidUploadResult()
      if (isResult) {
        const {loanAmount, loanTerm, productName} = loanInfo.value
        const params = {
          loanAmount, loanTerm, productName,
          productNameMain: productNameMain
        }
        Toast.loading({
          duration: 0,
          message: t('加载中')
        })
        const res = await applyConfirm(params)
        if (res.code === 1) {
          Toast({
            message: t('提交成功')
          })
          localStorage.setItem('orderId', res.data)
          Toast.clear()
          upLoadAdjustPoint({eventName: '1975d2'})
          router.replace('/oBVrmEUinQ')
        } else if (res.code === 6020) {
          const isResult = await getAndroidUploadResult()
          if (isResult) {
            const res = await applyConfirm(params)
            if (res.code === 1) {
              Toast({
                message: t('提交成功')
              })
              upLoadAdjustPoint({eventName: '1975d2'})
              localStorage.setItem('orderId', res.data)
              Toast.clear()
              router.replace('/oBVrmEUinQ')
            }
          }
        }
        Toast.clear()
      }
    }

    async function getLoanInfo() {
      const res = await fetchLoanProductDetails({productName: productName})
      if (res.code === 1) {
        loanInfo.value = {
          ...res.data,
        }
        planList.value = (res.data.planList || []).map(item => {
          item.refundDate = formatDate_EN(new Date(item.refundDate))
          return item
        })
        downTime.value = loanInfo.value.withdrawalCountdown ? (loanInfo.value.withdrawalCountdown * 60 * 1000) : (10 * 60 * 1000)
        productFeeShow.value = loanInfo.value.newLoanConfirmShow === 1
        // 清除loading
        store.dispatch('changeLoading', false)
      }
    }

    // 获取银行卡列表
    async function getBankList() {
      const res = await fetchMyCard()
      if (res.code === 1) {
        if (res.data.length !== 0) {
          selectBankList.value = res.data.sort((a, b) => b.isSelected - a.isSelected).map(item => {
            item.accountTypeName = item.accountType === 1 ? t('电子钱包') : t('银行卡')
            return item
          })
          bankInfo.bankcardName = res.data[0].bankcardName
          bankInfo.bankcard = res.data[0].bankcard
          bankInfo.accountTypeName = res.data[0].accountTypeName
        }
      }
    }

    // 点击切换银行卡
    function selectBankClick() {
      console.log(bankInfo)
      bankCardShow.value = true
      selectBankList.value = selectBankList.value.map(item => {
        item.name = item.bankcard
        item.subname = item.bankcardName
        item.disabled = bankInfo.bankcard === item.bankcard
        return item
      })
    }

    // 设置银行卡为默认收款银行
    async function onDefaultSelect() {
      console.log(selectBankList.value, bankInfo)
      const info = selectBankList.value.find(v => v.bankcard === bankInfo.bankcard)
      const params = {
        ...info,
        isSelected: 1,
      }
      await upDateMyCard(params)
    }

    // 选中银行卡
    function selectBankConfirm(value) {
      bankInfo.bankcardName = value.bankcardName
      bankInfo.bankcard = value.bankcard
      bankInfo.accountTypeName = value.accountTypeName
      onDefaultSelect()
    }

    onMounted(() => {
      setResourcesShow(8).then(res => {
        productInfoShow.value = res
      })
      getBankList()
      getLoanInfo()

    })
    return {
      loanInfo,
      bankCardShow,
      planList,
      selectBankClick,
      selectBankConfirm,
      selectBankList,
      bankInfo,
      expandInfoShow,
      confirmShow,
      downTime,
      apply,
      productInfoShow,
      productFeeShow,
      onDefaultSelect
    }
  }
}
</script>

<style lang="less" scoped>

.wrapper {
  padding: 15px;
  font-size: 16px;
  margin-top: 20px;
  margin-bottom: 80px;

  .product_box {
    border-radius: 10px;
    margin-bottom: 15px;
    .product_header {
      border-radius: 10px 10px 0 0;
      background-color: #BBF0F0;
      padding: 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-bottom: 20px;
      font-size: 18px;
      .amount{
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 24px;
        color: #E79A00;
        line-height: 34px;
        text-align: right;
        font-style: normal;
        text-transform: none;
      }
      .product_Logo {
        height: 40px;
        width: 40px;
        border-radius: 50%;
        vertical-align: middle;
      }
    }

    .loan_info {
      margin-top: -10px;
      padding: 0 15px;
      border-radius: 10px;
      background-color: #FFFFFF;
      .fee_content {
        color: #a8a8a8;
        font-size: 11px;
        line-height: 22px;
        padding: 10px 25px;
      }

      .loan_info_item {
        padding: 10px 0;
      }

      .expand {
        color: #999999;
        font-size: 14px;
      }

      .tips_text {
        color: #3851EC;
        font-size: 12px;
        padding:  10px;
      }
    }
  }

  .product_name {
    padding: 15px;
    background-color: #FFC99C;
    border-radius: 8px;
    font-size: 20px;
    width: 200px;
    margin: 0 auto;

    .amount {
      color: #FF7A00;
      text-align: center;
      font-size: 30px;
    }

    .product_logo {
      height: 50px;
      width: 50px;
      vertical-align: middle;
    }
  }


  .bank_card_box {
    box-sizing: border-box;
    display: flex;
    background-color: #FFFFFF;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
    justify-content: space-between;
    align-items: center;
    border-radius: 10px;

    .loan_card_num {
      flex: 1;
      text-align: right;
      font-family: Space Grotesk, Space Grotesk;
      font-size: 13px;
      padding-right: 5px;
    }

    .loan_bank_icon {
      height: 32px;
      width: 32px;
      margin-left: 8px;
    }

    .change_right_icon {
      height: 45px;
      line-height: 60px;
      padding: 0 10px;
      background-color: #BBF0F0;
      border-radius: 0 10px 10px 0;
    }

    .loan_card_right {
      height: 24px;
      width: 24px;
    }
  }

  .loanInfo_box {
    background-color: #FFFFFF;
    padding: 15px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
    border-radius: 8px;
    margin-bottom: 20px;
    position: relative;

    .header {
      position: absolute;
      left: 0px;
      top: 0px;
      border-radius: 10px;
      background: linear-gradient(to bottom, #FF7805, #FFBE87);
      padding: 14px;
    }

    .repay_plan {
      margin-top: 10px;
      font-size: 16px;
      position: relative;

      .content {
        border-radius: 10px;

        .content_item {
          padding: 10px 0;
        }
      }
    }

  }

}

.add_card_text {
  text-align: center;
  font-size: 14px;
  margin: 10px 0;
  color: #FF7805;
  height: 25px;
  line-height: 25px;
}

.confirm_btn {
  background-color: #FFFFFF;
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 15px 0;
  text-align: center;
}
</style>